/******************************************************************************
 **
 **    LISA Confidential
 **
 **    Source Materials
 **
 **    (C) COPYRIGHT LISA 2008-2009
 **
 **  File Name:      EquipFixRecord.java
 **  Description:
 **  Date:           2009-2-2
 **  Author:         LisaQian
 ******************************************************************************/
package com.ti.andon.common.web.action;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.taskmgmt.exe.TaskInstance;

import com.opensymphony.xwork2.ActionSupport;
import com.ti.andon.common.util.ADConstants;
import com.ti.andon.persistent.AdAlndomain;
import com.ti.andon.persistent.AdEquip;
import com.ti.andon.persistent.AdFixRecord;
import com.ti.andon.persistent.AdIssue;
import com.ti.andon.persistent.AdPerson;
import com.ti.andon.persistent.dao.AdAlndomainDAO;
import com.ti.andon.persistent.dao.AdEquipDAO;
import com.ti.andon.persistent.dao.AdFixRecordDAO;
import com.ti.andon.persistent.dao.AdIssueDAO;
import com.ti.andon.persistent.dao.AdIssueResponseDAO;
import com.ti.andon.persistent.dao.AdPersonDAO;
import com.ti.andon.persistent.dao.CommonDAO;
import com.ti.andon.persistent.dao.JbpmAdProcessLogDAO;
import com.ti.andon.workflow.WFConstants;
import com.ti.andon.workflow.WorkflowManager;
import com.ti.andon.workflow.WorkflowManagerImpl;

public class EquipFixRecord extends ActionSupport
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1145786149608166931L;

//	private static WorkflowManager workflowManager;
//	private static JbpmContext jbpmContext;
//	
//	static 
//	{
//		JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
//		jbpmContext = jbpmConfiguration.createJbpmContext();
//		workflowManager = new WorkflowManagerImpl(jbpmContext);
//	}
	
	public EquipFixRecord()
	{
		if (null == fixRecordDao)
		{
			fixRecordDao = new AdFixRecordDAO();
		}
		if (null == alnDomainDao)
		{
			alnDomainDao = new AdAlndomainDAO();
		}
		if (null == equipDao)
		{
			equipDao= new AdEquipDAO();
		}
		if (null == issueDAO)
		{
			issueDAO = new AdIssueDAO();
		}
		if (null == issueResponseDao)
		{
			issueResponseDao = new AdIssueResponseDAO();
		}
		if (null == personDao)
		{
			personDao = new AdPersonDAO();
		}
		if (null == commonDao)
		{
			commonDao = new CommonDAO();
		}
		if (null == jbpmProcessLogDao)
		{
			jbpmProcessLogDao = new JbpmAdProcessLogDAO();
		}
	}
	
	private AdFixRecord fixRecord;
	private Collection<AdFixRecord> fixRecords;
	private AdFixRecordDAO fixRecordDao;
	private Collection<AdAlndomain> shifts;
	private Collection<AdAlndomain> workgroups;
	private AdPersonDAO personDao;
	private Collection<AdPerson> persons;
	private AdAlndomainDAO alnDomainDao;
	private CommonDAO commonDao;
	private AdEquipDAO equipDao;
	private Collection<AdEquip> equips;
	private AdIssueDAO issueDAO;
	private List<AdIssue> issues;
	private AdIssueResponseDAO issueResponseDao;
	private JbpmAdProcessLogDAO jbpmProcessLogDao;
	// Attributes
	private Long fixRecordId;
	
	private Integer isResponsorDealing;
	
//	public String execute() throws Exception
//	{
//		//setMessage(getText(MESSAGE));
//		return SUCCESS;
//	}

	/**
	 * @return the fixRecord
	 */
	public AdFixRecord getFixRecord()
	{
		return fixRecord;
	}

	/**
	 * @param fixRecord the fixRecord to set
	 */
	public void setFixRecord(AdFixRecord fixRecord)
	{
		this.fixRecord = fixRecord;
	}

	/**
	 * @return the fixRecords
	 */
	public Collection<AdFixRecord> getFixRecords()
	{
		return fixRecords;
	}

	/**
	 * @param fixRecords the fixRecords to set
	 */
	public void setFixRecords(Collection<AdFixRecord> fixRecords)
	{
		this.fixRecords = fixRecords;
	}
	
	public String listTODO()
	{
		try
		{		
			// TODO to sets the actor id to the current login user id.
			
			
			JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
			JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
			WorkflowManager workflowManager = new WorkflowManagerImpl(jbpmContext);
			
			List taskList = workflowManager.getTaskListByActorIdProcDefName(new Long(1), "EquipmentFix");
			Iterator taskIter = taskList.iterator();
			fixRecords = new ArrayList();
			List list;
			while (taskIter.hasNext())
			{
				TaskInstance taskInstance = (TaskInstance)taskIter.next(); 
				String bizKey = taskInstance.getProcessInstance().getKey();
//				fixRecords.add(fixRecordDao.findById(new Long(Long.parseLong(bizKey))));
				list = fixRecordDao.findByProperty("fixRecordCode", bizKey);
				if (null != list && list.size()>0)
				{
					AdFixRecord fixRecord = (AdFixRecord)list.get(0);	
					fixRecords.add(fixRecord);
				}
			}
			
//			fixRecordDao.getSession().clear();
//			fixRecords.add(fixRecordDao.findById(Long.getLong(bizKey)));
			//List availableTxns = taskInstance.getAvailableTransitions();
			
			
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String listDone()
	{
		try
		{		
			// TODO to sets the actor id to the current login user id.
			List processedBizKeys = jbpmProcessLogDao.findDistinctBizKeys(new Long(1));
			Iterator bizKeysIter = processedBizKeys.iterator();
			fixRecords = new ArrayList();
			List list;
			while (bizKeysIter.hasNext())
			{
				String fixRecordCode = (String)bizKeysIter.next(); 				
				list = fixRecordDao.findByProperty("fixRecordCode", fixRecordCode);
				if (null != list && list.size()>0)
				{
					AdFixRecord fixRecord = (AdFixRecord)list.get(0);	
					fixRecords.add(fixRecord);
				}
			}
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String listDraft()
	{
		fixRecordDao.getSession().clear();
		fixRecords = fixRecordDao.findAllDraft();
		return SUCCESS;
	}
	
	public String add()
	{
		shifts = alnDomainDao.findAllShifts();
		workgroups = alnDomainDao.findAllWorkgroups();
		equips = equipDao.findAll();
		issues = issueDAO.findAll();
		persons = new ArrayList();
		// persons = personDao.findAll();
		
		fixRecord = new AdFixRecord();		
		long nextSeqId = commonDao.getNextSeqId(ADConstants.SEQUENCE_FIX_RECORD_CODE);
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMinimumIntegerDigits(7);
		nf.setGroupingUsed(false);		
		fixRecord.setFixRecordCode(nf.format(nextSeqId));
		
		return SUCCESS;
	}
	
	
	private void editRecord()
	{		
		shifts = alnDomainDao.findAllShifts();
		workgroups = alnDomainDao.findAllWorkgroups();
		equips = equipDao.findAll();
		issues = issueDAO.findAll();
		persons = new ArrayList();
		if (null != issues && issues.size()>0)
		{
			persons = issueResponseDao.findResponsersByIssueCode(((AdIssue)(issues.get(0))).getIssueCode());
		}
		
		fixRecord = fixRecordDao.findById(fixRecordId);
	}
	
	public String edit()
	{
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		session.setAttribute("isResponsorDealing", new Integer(0));
//		isResponsorDealing = "N";
		editRecord();
		
		return SUCCESS;		
	}
	
	
	public String responsorEdit()
	{
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		session.setAttribute("isResponsorDealing", new Integer(1));
		//isResponsorDealing = "Y";
		
		
		editRecord();
		
		return SUCCESS;	
	}
	
	public String delete()
	{
		fixRecordDao.getSession().beginTransaction();
		if (null != fixRecordId)
		{
			
			fixRecordDao.delete(fixRecordDao.findById(fixRecordId));
		}
		fixRecordDao.getSession().getTransaction().commit();
		
		return SUCCESS;
	}
	
	public String save()
	{
		System.out.println("***Now Saving...***");
		fixRecord.setRecStatus(ADConstants.FIX_RECORD_STATUS_DRAFT);
		fixRecordDao.getSession().beginTransaction();
		fixRecordDao.merge(fixRecord);
//		if (null == fixRecord.getFixRecordId())
//		{
//			fixRecordDao.save(fixRecord);
//		}
//		else
//		{
//			fixRecordDao.getSession().update(fixRecord);
//		}
		fixRecordDao.getSession().getTransaction().commit();
		
		System.out.println("fixRecord.getFixRecordId()="+fixRecord.getFixRecordId());
		// submit();
		System.out.println("***Saving Done!***");
		return SUCCESS;
	}

	public String submit()
	{
		System.out.println("***Now Submitting...***");
		try
		{
			fixRecord.setRecStatus(ADConstants.FIX_RECORD_STATUS_INPR);
			fixRecordDao.getSession().beginTransaction();
			fixRecordDao.merge(fixRecord);
			fixRecordDao.getSession().getTransaction().commit();
			
			JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
			JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
			WorkflowManager workflowManager = new WorkflowManagerImpl(jbpmContext);
			
			workflowManager.creatProcessInstance(new Long(0), WFConstants.PD_NAME_OF_EQUIPMENT_FIX, fixRecord
					.getFixRecordCode(), WFConstants.TSN_T_GOTO_INITIALIZATION, new Long(1), new Date());
			TaskInstance taskInstance = workflowManager.getCurrentTaskInstanceForOneUser(fixRecord.getFixRecordCode(),
					WFConstants.PD_NAME_OF_EQUIPMENT_FIX, new Long(0));
			taskInstance.getContextInstance().setVariable(WFConstants.NEXT_REQUESTER, fixRecord.getResponser1Id());
			workflowManager.transProcessInstance(fixRecord.getFixRecordCode(), WFConstants.PD_NAME_OF_EQUIPMENT_FIX,
					WFConstants.TSN_T_SUBMIT_TO_OPERATOR, new Long(0), "Test0314_02");
			jbpmContext.close();
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("***Submission Done!***");
		return SUCCESS;
	}
	
	public String done()
	{
		System.out.println("***Now Finishing...***");
		try
		{
			fixRecord.setRecStatus(ADConstants.FIX_RECORD_STATUS_DONE);
			fixRecordDao.getSession().beginTransaction();
			fixRecordDao.merge(fixRecord);		
			
			JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
			JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
			WorkflowManager workflowManager = new WorkflowManagerImpl(jbpmContext);
			
			
//			TaskInstance taskInstance = workflowManager.getCurrentTaskInstanceForOneUser(fixRecord.getFixRecordCode(),
//					WFConstants.PD_NAME_OF_EQUIPMENT_FIX, new Long(0));
			// taskInstance.getContextInstance().setVariable(WFConstants.NEXT_REQUESTER, fixRecord.getResponser1Id());
			workflowManager.transProcessInstance(fixRecord.getFixRecordCode(), WFConstants.PD_NAME_OF_EQUIPMENT_FIX,
					WFConstants.TSN_T_FINISH, new Long(1), "Test0314_03");
			jbpmContext.close();
			
			fixRecordDao.getSession().getTransaction().commit();
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("***Done!***");
		return SUCCESS;
	}
	
	/**
	 * @return the shifts
	 */
	public Collection<AdAlndomain> getShifts()
	{
		return shifts;
	}

	/**
	 * @param shifts the shifts to set
	 */
	public void setShifts(Collection<AdAlndomain> shifts)
	{
		this.shifts = shifts;
	}

	/**
	 * @return the workgroups
	 */
	public Collection<AdAlndomain> getWorkgroups()
	{
		return workgroups;
	}

	/**
	 * @param workgroups the workgroups to set
	 */
	public void setWorkgroups(Collection<AdAlndomain> workgroups)
	{
		this.workgroups = workgroups;
	}

	/**
	 * @return the equips
	 */
	public Collection<AdEquip> getEquips()
	{
		return equips;
	}

	/**
	 * @param equips the equips to set
	 */
	public void setEquips(Collection<AdEquip> equips)
	{
		this.equips = equips;
	}

	/**
	 * @return the issues
	 */
	public List<AdIssue> getIssues()
	{
		return issues;
	}

	/**
	 * @param issues the issues to set
	 */
	public void setIssues(List<AdIssue> issues)
	{
		this.issues = issues;
	}

	/**
	 * @return the persons
	 */
	public Collection<AdPerson> getPersons()
	{
		return persons;
	}

	/**
	 * @param persons the persons to set
	 */
	public void setPersons(Collection<AdPerson> persons)
	{
		this.persons = persons;
	}

	/**
	 * @return the fixRecordId
	 */
	public Long getFixRecordId()
	{
		return fixRecordId;
	}

	/**
	 * @param fixRecordId the fixRecordId to set
	 */
	public void setFixRecordId(Long fixRecordId)
	{
		this.fixRecordId = fixRecordId;
	}
}
